<?php
/**
 * 文章评论
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/21
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;

class ArticleMessageModel extends Model
{
    const SHOW_STATUS=1; //正常
    const HIDE_STATUS=0; //禁用
    const DEL_STATUS=-1; //删除

    protected $autoWriteTimestamp = true;

    public function user()
    {
        return $this->hasOne('MemberModel','uid','user_id')->field('uid,mobile');
    }

    public function article()
    {
        return $this->hasOne('ArticleModel','article_id','article_id')->field('article_id,title');
    }

    /**
     * 所有评论
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::with('user')
            ->with('article')
            ->where('status','>',self::DEL_STATUS)
            ->where($where)
            ->order('status asc,create_time desc')
            ->paginate($pageSum);

        foreach ($list as $vo){
            $vo->pcontent='';

            if($vo->pid>0){
                $con=self::field('id,content')->find($vo->pid);
                $vo->pcontent=$con->content;
            }
        }

        return $list;
    }


    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $au=self::get($id);
        $au->status=self::DEL_STATUS;

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    /**
     * 增加留言
     * @param $id
     * @param $uid
     * @param $content
     * @param $pid
     * @return array
     */
    public function addArticle($id,$uid,$content,$pid){
        $autoTime=getSystem("auto_push_time");
        $publish_time=time()+(60*$autoTime);

        $data = [
            "article_id"=>$id,
            "pid"=>$pid,
            "user_id"=>$uid,
            "content"=>$content,
            "status"=>1,
            'publish_time' => $publish_time,
            'create_time' => time(),
        ];
        $status = self::insertGetId($data);
        if($status){
            UserActionModel::adds($uid,$id,1,'msg',$status);

            //佣金
            if($pid>0){
                $info=self::find($pid);
                if(!empty($info) && $uid!=$info["user_id"]){
                    MemberModel::pushGold($uid,2,$status);
                }
            }else{
                MemberModel::pushGold($uid,2,$status);
            }
            return ['status'=>1,'msg'=>'增加成功！'];
        }else{
            return ['status'=>0,'msg'=>'增加失败！'];
        }
    }

    /**
     * 获取用户留言
     * @param $id
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserList($id,$pageSum = 10){
        if ($pageSum <= 0) {
            $pageSum = getSystem("pageSUM") ? getSystem("pageSUM") : 10;
        }

        $list=self::with("user")
            ->where('status',self::SHOW_STATUS)
            ->where('article_id',$id)
            ->where('publish_time', '<=',time())
            ->order('create_time desc')
            ->paginate($pageSum);

        foreach ($list as $vo){
            $arr=$this->checkChild($vo);
            $vo->first=$arr[count($arr)-1];

            if(count($arr)>1){
                unset($arr[count($arr)-1]);
                $vo->child=$arr;
            }else{
                $vo->child=[];
            }
        }

        return $list;
    }

    /**
     * 循环所有回复
     * @param $item
     * @return array
     * @throws \think\exception\DbException
     */
    private function checkChild($item){
        $list=self::with("user")
            ->where("id",$item->pid)
            ->where('status',self::SHOW_STATUS)
            ->select();

        foreach ($list as $vo){
            $data=$this->checkChild($vo);
        }
        $data[]=$item->toArray();

        return $data;
    }
} 